<template>
  <div class="order-container">
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <!-- 搜索区域 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" clearable v-model="queryInfo.query" @change="serchOrder">
            <el-button icon="el-icon-search" slot="append" @click="serchOrder"></el-button>
          </el-input>
        </el-col>
      </el-row>

      <!-- 表格区域 -->
      <el-table :data="orderList" border stripe>
        <el-table-column label="索引" type="index" width="60"></el-table-column>
        <el-table-column label="订单编号" prop="order_number" width="280"></el-table-column>
        <el-table-column label="订单价格" prop="order_price"></el-table-column>
        <el-table-column label="是否支付" prop="order_pay" width="80">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.order_pay === '1'">已支付</el-tag>
            <el-tag type="danger" v-else>未支付</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否发货" prop="is_send"></el-table-column>
        <el-table-column label="下单时间" prop="create_time">
          <template slot-scope="scope">
            {{scope.row.create_time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作" :min-width="180">
          <template slot-scope="scope">
            <el-button type="primary" round size="mini" icon="el-icon-edit" @click="showAreaDialog"></el-button>
            <el-button type="success" round size="mini" icon="el-icon-s-tools" @click="showProgressDialog(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="pageSizeChange"
        @current-change="pageCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 8, 10, 15]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
    <!-- 修改地址对话框 -->
    <el-dialog
      title="修改地址"
      :visible.sync="areaVisible"
      width="50%">
      <!-- 表单区域 -->
      <el-form :model="areaForm" :rules="areaFormRule" ref="areaFormRef" :label-width="70">
        <el-form-item label="省市区/县" prop="area">
          <!-- 级联选择器 -->
          <el-cascader
            v-model="areaForm.area"
            :options="cityData"
            :props="cityProp"
            @change="cascaderChange"></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="detialaddr" >
          <el-input v-model="areaForm.detialaddr" class="detail-input"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="areaDialogClosed">取 消</el-button>
        <el-button type="primary" @click="areaDialogClosed">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 物流进度对话框 -->
    <el-dialog
      title="物流进度"
      :visible.sync="progVisible"
      width="50%">
      <el-alert>快递一百对应后端接口报错</el-alert>
      <!-- 时间线 -->
      <el-timeline>
        <el-timeline-item
          v-for="(activity, index) in activities"
          :key="index"
          :timestamp="activity.timestamp">
          {{activity.content}}
        </el-timeline-item>
      </el-timeline>
      <span slot="footer" class="dialog-footer">
        <el-button @click="progVisible = false">取 消</el-button>
        <el-button type="primary" @click="progVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getAllOrder, serchOrder } from '@/api/orderApi.js'
import cityData from '@/assets/scripts/city_data.js'
// import { getDeliveryInfo } from '@/api/deliveryInfoApi.js'
export default {
  name: 'myOrder',
  data () {
    return {
      // 订单列表
      orderList: [],
      // 获取订单的相关参数
      queryInfo: {
        pagenum: 1,
        pagesize: 5,
        // 搜索输入框的内容
        query: ''
      },
      // 总订单数
      total: 0,
      // 修改地址对话框显隐控件
      areaVisible: false,
      // 城市数据
      cityData,
      // 级联选择器配置对象
      cityProp: {
        expandTrigger: 'hover',
        label: 'label',
        value: 'value',
        children: 'children',
        multiple: false
      },
      // 修改地址表单对象
      areaForm: {
        area: [],
        detailaddr: ''
      },
      // 修改地址表单校验
      areaFormRule: {
        area: [
          { required: true, message: '请选择地区', trigger: 'blur' }
        ],
        detialaddr: [
          { required: true, message: '请输入详细地址', trigger: 'blur' }
        ]
      },
      // 物流进度对话框显隐控件
      progVisible: false,
      // 时间线数据
      activities: [{
        content: '创建订单',
        timestamp: '2018-04-15'
      }, {
        content: '通过审核',
        timestamp: '2018-04-13'
      }, {
        content: '开始发货',
        timestamp: '2018-04-11'
      }]
    }
  },
  methods: {
    // 搜索输入框内容变化事件和搜索按钮点击事件
    async serchOrder () {
      const { data: res } = await serchOrder(this.queryInfo.pagenum, this.queryInfo.pagesize, this.queryInfo.query)
      console.log(res)
      if (res.meta.status !== 200) {
        return this.$notify.error({ title: '查找失败', message: res.meta.msg })
      }
      // 否则查找成功
      this.orderList = res.data.goods
      this.total = res.data.total
    },
    // 获取订单列表
    async initOrderList () {
      const { data: res } = await getAllOrder(this.queryInfo)
      if (res.meta.status !== 200) {
        return this.$notify.error({ title: '获取订单列表失败', message: res.meta.msg })
      }
      // 否则获取成功
      this.orderList = res.data.goods
      this.total = res.data.total
    },
    // 页面信息条数变化
    pageSizeChange (cpsize) {
      // 将$event重命名为cpsize并刷新给queryinfo.pagesize
      this.queryInfo.pagesize = cpsize
      // 初始化订单信息
      this.initOrderList()
    },
    // 当前页数变化
    pageCurrentChange (cpnum) {
      // 将$event重命名为cpbum并刷新给queryinfo.pagenum
      this.queryInfo.pagenum = cpnum
      // 初始化订单信息
      this.initOrderList()
    },
    // 修改地址对话框对应激活按钮的点击事件
    showAreaDialog () {
      this.areaVisible = true
    },
    // 级联选择器选中值变化
    cascaderChange (newValue) {
      console.log(newValue)
    },
    // 选择地区表单关闭事件
    areaDialogClosed () {
      this.areaForm.area = []
      this.areaForm.detailaddr = ''
      this.areaVisible = false
    },
    // 显示物流状态对话框的事件
    async showProgressDialog (row) {
      console.log(row.order_number)
      // const { data: res } = await getDeliveryInfo()
      // console.log(res)
      this.progVisible = true
    }
  },
  created () {
    this.initOrderList()
  }
}
</script>

<style lang="less" scoped>
.el-table {
  margin: 20px 0;
}
.detail-input {
  max-width: 217px;
}
.el-timeline {
  margin: 20px 0;
}
</style>
